package org.jeecg.modules.mq.service.impl;


import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.mq.constant.RabbitConstants;
import org.jeecg.modules.mq.service.OrderProducerService;
import org.jeecg.modules.order.entity.MtRepairOrder;
import org.jeecg.modules.order.vo.MtRepairOrderPage;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;

@Service
@Slf4j
public class OrderProducerServiceImpl implements OrderProducerService {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    public boolean sendTopicOrderMessage(MtRepairOrderPage mtRepairOrderPage) {
        if (Objects.nonNull(mtRepairOrderPage)) {
            try {
                String messageId = String.valueOf(UUID.randomUUID());
                String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
                Map<String, Object> orderMap = new HashMap<>();
                orderMap.put("messageId", messageId);
                orderMap.put("messageData", mtRepairOrderPage);
                orderMap.put("createTime", createTime);
                rabbitTemplate.convertAndSend(RabbitConstants.ORDER_EXCHANGE, "order.info", orderMap,correlationData->{
                    correlationData.getMessageProperties().setExpiration("3000");//设置消息的ttl：3秒
                    return correlationData;
                });
                return true;
            } catch (Exception e) {
                return false;
            }
        }
        return false;
    }
}
